package com.example.blog.web;

import com.example.blog.po.Blog;
import com.example.blog.po.Type;
import com.example.blog.service.TypeService;
import com.github.pagehelper.PageHelper;
import com.example.blog.service.BlogService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

@Controller
public class indexController {
    @Autowired
    BlogService blogService;
    @Autowired
    TypeService typeService;

    @GetMapping("/")
    public String indexPage(@RequestParam(defaultValue = "1", value = "pageNum") Integer pageNum, Model model){
//        查询全部博客、分类、推荐...并传递到前台
        PageHelper.startPage(pageNum, 2);
        List<Blog> blogs = blogService.findAllBlog();
//        List<Blog> blogs = blogService.findPublishedBlog();
        PageInfo<Blog> pageInfo = new PageInfo<>(blogs);

        List<Type> types = typeService.findAllTypes();

        model.addAttribute("pageInfo", pageInfo);
        model.addAttribute("types", types);
//        String blog = null;
//        if(blog == null){
//            throw new NotFoundException("博客不存在");
//        }
        System.out.println("--------index---------");
        return "index";
    }

//    @RequestMapping(value="/{module}/{module2}/{name}")
//    public String commonController(@PathVariable String module,@PathVariable String module2, @PathVariable String name) {
//
//        return module + "/" + module2 + "/" + name;
//    }
//
//
//    @RequestMapping(value="/{module}/{name}")
//    public String commonController2(@PathVariable String module, @PathVariable String name) {
//        System.out.println("-----2层-----");
//        return module+"/"+name;
//    }
//
//    @RequestMapping(value="/{name}")
//    public String commonController(@PathVariable String name) {
//
//        return name;
//    }
}
